//
//  YYESendNotesViewController.m
//  YuYingEDU
//
//  Created by 胡波 on 2018/1/13.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYESendNotesViewController.h"
#import "UITextView+Placeholder.h"

@interface YYESendNotesViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic,strong) UITextView *contentTV;
@property (nonatomic,strong) UIButton *chooseBtn;
@property (nonatomic,strong) UIImage *chooseIV;

@end

@implementation YYESendNotesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"发布";
    
    [self configUI];
    
    [self configNavi];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (@available(iOS 11.0, *)) {
        UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
    } else {
        // Fallback on earlier versions
    }
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    if (@available(iOS 11.0, *)) {
        
        UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

     } else {
        // Fallback on earlier versions
    }
    
}

- (void)configUI {
    
    self.contentTV = [[UITextView alloc]init];
    self.contentTV.placeholder = @"分享心得笔记";
    self.contentTV.placeholderLabel.font = [UIFont systemFontOfSize:14];
    self.contentTV.placeholderLabel.textColor = LineColor;
    self.contentTV.font = [UIFont systemFontOfSize:14];
    self.contentTV.textColor = [UIColor blackColor];
    self.contentTV.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.contentTV];
    
    self.chooseBtn = [[UIButton alloc]init];
    [self.chooseBtn setImage:[UIImage imageNamed:@"chooseIV"] forState:UIControlStateNormal];
    [self.chooseBtn addTarget:self action:@selector(chooseClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.chooseBtn];
    
    [self.contentTV makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(18);
        make.right.equalTo(self.view.right).offset(-18);
        make.top.equalTo(self.view.top);
        make.height.equalTo(SCREEN_WIDTH * 0.53);
    }];
    
    [self.chooseBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(18);
        make.top.equalTo(self.contentTV.bottom).offset(18);
        make.size.equalTo(CGSizeMake(105, 105));
    }];
    
    
}

- (void)configNavi {
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发布" style:UIBarButtonItemStyleDone target:self action:@selector(sendClicked)];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor blackColor]];
    
}

// 发布
- (void)sendClicked {
    
    
    if (self.contentTV.text.length == 0) {
        [self.view makeToast:@"请输入心得"];
        return;
    }
    
    NSDictionary *dic = @{
                          @"uid":[LoginModel shareLogin].uid,
                          @"username":[LoginModel shareLogin].username,
                          @"token":[LoginModel shareLogin].token,
                          @"note":self.contentTV.text
                          };
    [SVProgressHUD show];
    [AFNHelper post:SendNotes parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        if (self.chooseIV) {
            [formData appendPartWithFileData:UIImageJPEGRepresentation(self.chooseIV, 0.3) name:@"image" fileName:@"content.png" mimeType:@"png"];
        }
        
    } success:^(id responseObject) {
        
        NSLog(@"%@",responseObject);
        if ([responseObject[@"status"] isEqual:@1]) {
            [[NSNotificationCenter defaultCenter]postNotificationName:@"SendCompleteRefreshData" object:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }
        [SVProgressHUD showWithStatus:responseObject[@"message"]];
        [SVProgressHUD dismiss];
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        [SVProgressHUD showWithStatus:ShowError];
        [SVProgressHUD dismissWithDelay:0.5];
        
    }];
    
    
}

// 选择图片
- (void)chooseClicked {
    
    UIImagePickerController *pickerVC = [[UIImagePickerController alloc]init];
    pickerVC.delegate = self;
    //    pickerVC.allowsEditing = NO;
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"手机拍摄" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        pickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
        //        pickerVC.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        [self presentViewController:pickerVC animated:YES completion:nil];
        
    }];
    
    UIAlertAction *photosAction = [UIAlertAction actionWithTitle:@"相册选择" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        pickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:pickerVC animated:YES completion:nil];
        
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        [alertVC addAction:cameraAction];
        
    }
    [alertVC addAction:photosAction];
    [alertVC addAction:cancelAction];
    [self presentViewController:alertVC animated:YES completion:nil];
    
}

#pragma mark -- UIImagepickerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    [self.chooseBtn setImage:image forState:UIControlStateNormal];
    self.chooseIV = image;
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
